home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWMemory / SLMemMgr.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  12.1 KB  |  415 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SLMemMgr.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef   SLMEMMGR_H
  13. #include "SLMemMgr.h"
  14. #endif
  15.  
  16. #ifndef FWDEBUG_H
  17. #include "FWDebug.h"
  18. #endif
  19.  
  20. #ifndef FWEXCDEF_H
  21. #include "FWExcDef.h"
  22. #endif
  23.  
  24. #ifndef FWMEMHLP_H
  25. #include "FWMemHlp.h"
  26. #endif
  27.  
  28. #ifndef FWNEW_H
  29. #include "FWNew.h"
  30. #endif
  31.  
  32. #if defined(FW_BUILD_MAC) && !defined(__OSUTILS__)
  33. #include <OSUtils.h>
  34. #endif
  35.  
  36. #if defined(FW_BUILD_MAC) && !defined(__MEMORY__)
  37. #include <Memory.h>
  38. #endif
  39.  
  40. #if defined(FW_BUILD_WIN) && !defined(_INC_WINDOWS)
  41. #include <Windows.h>
  42. #endif
  43.  
  44. #if defined(FW_BUILD_WIN) && !defined(_INC_WINDOWSX)
  45. #include <WindowsX.h>
  46. #endif
  47.  
  48. #if !defined(FW_qUsePlatformAlloc) && !defined(FW_qUseCRuntimeAlloc)
  49.  
  50. #ifdef FW_BUILD_WIN
  51. #include <MMStubs.h>
  52. #endif
  53.  
  54. #ifdef FW_BUILD_MAC
  55. #include <MemMgr.h>
  56. #endif
  57.  
  58. #endif
  59.  
  60. #include <string.h>
  61.  
  62. #if defined(__SC__) && defined(FW_BUILD_WIN)
  63.  
  64. #if !defined(__LIMITS_H)
  65. #include <limits.h>
  66. #endif
  67.  
  68. #if !defined(__HUGEPTR_H) && defined(FW_BUILD_WIN16)
  69. #include <hugeptr.h>
  70. #endif
  71.  
  72. #endif
  73.  
  74. #define PRIV_CLEAR_MEM_ON_ALLOC
  75.  
  76. // It'd be cool to let this be set at program startup using InitializationFile setting
  77. #ifdef PRIV_CLEAR_MEM_ON_ALLOC
  78. const unsigned char kKnownRawByte = 0;
  79. #else
  80. const unsigned char kKnownRawByte = 0x5A;
  81. #endif
  82.  
  83. #ifdef FW_BUILD_MAC
  84. #pragma segment FWMemory
  85. #endif
  86.  
  87. //========================================================================================
  88. // Global procedures
  89. //========================================================================================
  90.  
  91. FW_EXTERN_C_BEGIN
  92.  
  93. //========================================================================================
  94. // CLASS FW_CMemoryManager
  95. //========================================================================================
  96.  
  97. //----------------------------------------------------------------------------------------
  98. // FW_PrivMemoryManager_AddOffsetToPointer
  99. //----------------------------------------------------------------------------------------
  100.  
  101. void *FW_PrivMemoryManager_AddOffsetToPointer(void *pointer, unsigned long offset)
  102. {
  103.     // No try block necessary - Do not throw
  104.  
  105. #if defined(FW_BUILD_MAC) || defined(FW_BUILD_WIN32)
  106.     return (void *)((char *)pointer + offset);
  107. #elif defined(__SC__) && defined(FW_BUILD_WIN16)
  108.     // If 'offset' is not less than the maximum signed LONG Value, consider it an fError.
  109.     // We could program for these cases, but it is More than likely an fError.
  110.     FW_ASSERT(offset < (unsigned long)LONG_MAX);
  111.     
  112.     // Symantec C++ note: hugeptr_add(p,o) is a macro that calls the function:
  113.     //        hugeptr_add(p, sizeof(*p) * o)
  114.     // Thus, the pointer passed to hugeptr_add() must be 'char *' type.
  115.     char * typedPointer                = pointer;
  116.     signed long signedLongOffset    = offset;
  117.     
  118.     // The call to hugeptr_add() is not scoped with '::' because Symantec C++
  119.     // first uses a macro and the macro fails to compile when scoped with '::'.
  120.     return hugeptr_add(typedPointer, signedLongOffset);
  121. #else
  122.     return (char __huge* pointer) + offset;
  123. #endif
  124. }
  125.  
  126. //----------------------------------------------------------------------------------------
  127. // FW_PrivMemoryManager_CopyMemory
  128. //----------------------------------------------------------------------------------------
  129. void FW_PrivMemoryManager_CopyMemory(const void* const source,
  130.                                      void* const destination,
  131.                                      unsigned long bytesToMove)
  132. {
  133.     // No try block necessary - Do not throw
  134.  
  135.     FW_ASSERT(source != 0);
  136.     FW_ASSERT(destination != 0);
  137.  
  138. #if defined(FW_BUILD_MAC)
  139.     ::BlockMoveData(source, destination, bytesToMove);
  140. #elif defined(FW_BUILD_WIN)
  141.     ::FW_PrimitiveCopyMemory(source, destination, bytesToMove);
  142. #endif
  143. }
  144.  
  145. //----------------------------------------------------------------------------------------
  146. // FW_PrivMemoryManager_SetMemory
  147. //----------------------------------------------------------------------------------------
  148. void FW_PrivMemoryManager_SetMemory(void *aBlock,
  149.                                     unsigned long bytesToSet,
  150.                                     unsigned char byteValue)
  151. {
  152.     // No try block necessary - Do not throw
  153.  
  154.     FW_ASSERT(aBlock != 0);
  155.     FW_PrimitiveSetMemory(aBlock, bytesToSet, byteValue);
  156. }
  157.  
  158. //----------------------------------------------------------------------------------------
  159. // FW_PrivMemoryManager_AllocateBlock - Allocate a non-relocatable block of fMemory.
  160. //----------------------------------------------------------------------------------------
  161. void* FW_PrivMemoryManager_AllocateBlock(unsigned long bytesRequested)
  162. {
  163.     // No try block necessary - Do not throw
  164.  
  165.     void* aBlock = FW_PrimitiveAllocateBlock(bytesRequested);
  166.  
  167. #ifdef FW_DEBUG
  168.     if (aBlock != 0)
  169.         FW_PrivMemoryManager_SetMemory(aBlock, bytesRequested, kKnownRawByte);
  170. #endif
  171.  
  172.     return aBlock;
  173. }
  174.  
  175. #ifdef FW_BUILD_MAC
  176. #pragma segment FWMemMgr2
  177. #endif
  178.  
  179. //----------------------------------------------------------------------------------------
  180. // FW_PrivMemoryManager_ResizeBlock - Resize a non-relocatable block of fMemory.  The
  181. //    block may be moved to a new location if necessary.
  182. //----------------------------------------------------------------------------------------
  183. void *FW_PrivMemoryManager_ResizeBlock(void *aBlock, unsigned long bytesRequested)
  184. {
  185.     // No try block necessary - Do not throw
  186.  
  187.     void *newBlock;
  188.     
  189.     if (aBlock == 0)
  190.         newBlock = FW_PrimitiveAllocateBlock(bytesRequested);
  191.     else
  192.         newBlock = FW_PrimitiveResizeBlock(aBlock, bytesRequested);
  193.  
  194.     return newBlock;
  195. }
  196.  
  197. //----------------------------------------------------------------------------------------
  198. // FW_PrivMemoryManager_GetBlockSize - Return the size of a non-relocatable block.
  199. //----------------------------------------------------------------------------------------
  200. unsigned long FW_PrivMemoryManager_GetBlockSize(const void* aBlock)
  201. {
  202.     // No try block necessary - Do not throw
  203.  
  204.     return FW_PrimitiveGetBlockSize((void*)aBlock);
  205. }
  206.  
  207. //----------------------------------------------------------------------------------------
  208. // FW_PrivMemoryManager_FreeBlock - Return a non-relocatable block to the free store.
  209. //----------------------------------------------------------------------------------------
  210. void FW_PrivMemoryManager_FreeBlock(void *aBlock)
  211. {
  212.     // No try block necessary - Do not throw
  213.  
  214.     FW_PrimitiveFreeBlock(aBlock);
  215. }
  216.  
  217. //----------------------------------------------------------------------------------------
  218. // FW_PrivMemoryManager_AllocateSystemHandle - 
  219. //----------------------------------------------------------------------------------------
  220. FW_PlatformHandle FW_PrivMemoryManager_AllocateSystemHandle(unsigned long bytesNeeded)
  221. {
  222.     // No try block necessary - Do not throw
  223.  
  224.     FW_PlatformHandle aSystemHandle;
  225.  
  226. #if !defined FW_qUsePlatformAlloc
  227.     aSystemHandle = (FW_PlatformHandle) ::MMAllocateHandle(bytesNeeded);
  228. #elif defined FW_BUILD_MAC
  229.     aSystemHandle = ::NewHandle(bytesNeeded);
  230. #elif defined FW_BUILD_WIN
  231.     aSystemHandle = ::GlobalAlloc(GMEM_MOVEABLE, bytesNeeded);
  232. #endif
  233.     
  234.     return aSystemHandle;
  235. }
  236.  
  237. #ifdef FW_BUILD_MAC
  238. #pragma segment FWMemMgr3
  239. #endif
  240.  
  241. //----------------------------------------------------------------------------------------
  242. // FW_PrivMemoryManager_ResizeSystemHandle - 
  243. //----------------------------------------------------------------------------------------
  244. FW_PlatformHandle FW_PrivMemoryManager_ResizeSystemHandle(FW_PlatformHandle aHandle,
  245.                                                           unsigned long bytesNeeded)
  246. {
  247.     // No try block necessary - Do not throw
  248.  
  249.     FW_ASSERT(aHandle != 0);
  250.  
  251.     FW_PlatformHandle newHandle = aHandle;
  252.  
  253. #if !defined FW_qUsePlatformAlloc
  254.     ::MMSetHandleSize(aHandle, bytesNeeded);
  255. #elif defined FW_BUILD_MAC
  256.     ::SetHandleSize(aHandle, bytesNeeded);
  257. #elif defined FW_BUILD_WIN
  258.     newHandle = ::GlobalReAlloc(aHandle, bytesNeeded, GMEM_MOVEABLE);
  259. #endif
  260.     
  261.     if (newHandle != 0 && FW_PrivMemoryManager_GetSystemHandleSize(newHandle) < bytesNeeded)
  262.     {
  263.         // If a new handle was allocated, don't destroy the old handle.  This 
  264.         // does make it possible aHandle to become a bad pointer under FW_BUILD_WIN 
  265.         // if the ::GlobalReAlloc() did realloc to a size less than "bytesNeeded".
  266.         // The client would hold onto the now invalid aHandle and think that it was 
  267.         // copacetic when the resize failed.  But I don't think ::GlobalReAlloc() 
  268.         // would return a handle smaller than the size requested.
  269.         if (newHandle != aHandle)
  270.             FW_PrivMemoryManager_FreeSystemHandle(newHandle);
  271.         newHandle = 0;
  272.     }
  273.  
  274.     return newHandle;
  275. }
  276.  
  277. #ifdef FW_BUILD_MAC
  278. #pragma segment FWMemMgr2
  279. #endif
  280.  
  281. //----------------------------------------------------------------------------------------
  282. // FW_PrivMemoryManager_FreeSystemHandle - 
  283. //----------------------------------------------------------------------------------------
  284. void FW_PrivMemoryManager_FreeSystemHandle(FW_PlatformHandle aHandle)
  285. {
  286.     // No try block necessary - Do not throw
  287.  
  288. #if !defined FW_qUsePlatformAlloc
  289.     if (aHandle)
  290.         ::MMFreeHandle(aHandle);
  291. #elif defined FW_BUILD_MAC
  292.     if (aHandle)
  293.         ::DisposeHandle(aHandle);
  294. #elif defined FW_BUILD_WIN
  295.     if (aHandle)
  296.         ::GlobalFree(aHandle);
  297. #endif
  298. }
  299.  
  300. #ifdef FW_BUILD_MAC
  301. #pragma segment FWMemMgr4
  302. #endif
  303.  
  304. //----------------------------------------------------------------------------------------
  305. // FW_PrivMemoryManager_LockSystemHandle - 
  306. //----------------------------------------------------------------------------------------
  307. void * FW_PrivMemoryManager_LockSystemHandle(FW_PlatformHandle handle)
  308. {
  309.     // No try block necessary - Do not throw
  310.  
  311.     FW_ASSERT(handle != 0);
  312.  
  313.     void* memory;
  314.     
  315. #if !defined FW_qUsePlatformAlloc
  316.     memory = ::MMLockHandle(handle);
  317. #elif defined FW_BUILD_MAC
  318.     HLock(handle);
  319.     memory = *handle;
  320. #elif defined FW_BUILD_WIN
  321.     memory = ::GlobalLock(handle);
  322. #endif
  323.  
  324.     return memory;
  325. }
  326.  
  327. #ifdef FW_BUILD_MAC
  328. #pragma segment FWMemMgr5
  329. #endif
  330.  
  331. //----------------------------------------------------------------------------------------
  332. // FW_PrivMemoryManager_UnlockSystemHandle - 
  333. //----------------------------------------------------------------------------------------
  334. void FW_PrivMemoryManager_UnlockSystemHandle(FW_PlatformHandle aHandle)
  335. {
  336.     // No try block necessary - Do not throw
  337.  
  338.     FW_ASSERT(aHandle != 0);
  339.     
  340. #if !defined FW_qUsePlatformAlloc
  341.     ::MMUnlockHandle(aHandle);
  342. #elif defined FW_BUILD_MAC
  343.     ::HUnlock(aHandle);
  344. #elif defined FW_BUILD_WIN
  345.     ::GlobalUnlock(aHandle);
  346. #endif
  347. }
  348.  
  349.  
  350. #ifdef FW_BUILD_MAC
  351. #pragma segment FWMemMgr6
  352. #endif
  353.  
  354. //----------------------------------------------------------------------------------------
  355. // FW_PrivMemoryManager_GetSystemHandleSize - 
  356. //----------------------------------------------------------------------------------------
  357. unsigned long FW_PrivMemoryManager_GetSystemHandleSize(FW_PlatformHandle aHandle)
  358. {
  359.     // No try block necessary - Do not throw
  360.  
  361.     FW_ASSERT(aHandle != 0);
  362.     
  363. #if !defined FW_qUsePlatformAlloc
  364.     return ::MMGetHandleSize(aHandle);
  365. #elif defined FW_BUILD_MAC
  366.     return ::GetHandleSize(aHandle);
  367. #elif defined FW_BUILD_WIN
  368.     return ::GlobalSize(aHandle);
  369. #endif
  370. }
  371.  
  372. //----------------------------------------------------------------------------------------
  373. // FW_PrivMemoryManager_CopySystemHandle - 
  374. //----------------------------------------------------------------------------------------
  375. FW_PlatformHandle FW_PrivMemoryManager_CopySystemHandle(FW_PlatformHandle aHandle)
  376. {
  377.     FW_PlatformHandle newHandle = NULL;
  378.     FW_VOLATILE(newHandle);
  379.     
  380. #if !defined FW_qUsePlatformAlloc
  381.     newHandle = (FW_PlatformHandle) ::MMCopyHandle(aHandle);
  382. #else
  383.     // We need this try block soley for emulated exceptions.
  384.     // The code that follows won't fail with an exception,
  385.     // but any shared library entry point that creates autodestruct objects
  386.     // needs to be enclosed in a try block.
  387.     FW_TRY
  388.     {
  389.         unsigned long size = GetSystemHandleSize(aHandle);
  390.     
  391.         FW_CAcquireLockedSystemHandle sourceLock(aHandle);
  392.         void *sourcePointer = sourceLock.GetPointer();
  393.     
  394.         newHandle = FW_PrivMemoryManager_AllocateSystemHandle(size);
  395.         if (newHandle != 0)
  396.         {
  397.             FW_CAcquireLockedSystemHandle destinationLock(newHandle);
  398.             void *destinationPointer = destinationLock.GetPointer();
  399.     
  400.             FW_PrivMemoryManager_CopyMemory(sourcePointer, destinationPointer, size);
  401.         }
  402.     }
  403.     FW_CATCH_BEGIN
  404.     FW_CATCH_EVERYTHING()
  405.     {
  406.         return NULL;
  407.     }
  408.     FW_CATCH_END
  409. #endif
  410.  
  411.     return newHandle;
  412. }
  413.  
  414. FW_EXTERN_C_END
  415.